package cn.xmkeshe.controller;

import cn.xmkeshe.back.ICustomerServiceBack;
import cn.xmkeshe.front.ICustomerServiceFront;
import cn.xmkeshe.model.Cars;
import cn.xmkeshe.model.Customer;
import cn.xmkeshe.utils.Log;
import cn.xmkeshe.utils.md5.MD5Code;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

@Controller
public class CustomerController {

  @Resource
  private ICustomerServiceFront iCustomerServiceFront;
  @Resource
  private ICustomerServiceBack iCustomerServiceBack;

  @RequestMapping("/")
  @Log(operationName = "用户首页",operationType = "系统")
  public String indexPre(){
    return "/pages/index";
  }

//  前台注册
  @RequestMapping("/add")
  @ResponseBody
  @Log(operationName = "用户注册",operationType = "用户")
  public String add(Customer customer){
    String flag = "error";
    customer.setCredate(new Timestamp(new Date().getTime())); // 当前日期
    customer.setPhoto("user.png");
    customer.setPassword(new MD5Code().getMD5ofStr("{[" + customer.getPassword() + "]}" + customer.getCuid()));
    if(iCustomerServiceFront.add(customer)){
      flag = "success";
  }
    return flag;
}

//  前台邮箱注册
  @RequestMapping("/email")
  @ResponseBody
  @Log(operationName = "用户邮箱注册",operationType = "用户")
  public Map<String, Object> createAccount(Customer customer){
    Map<String, Object> account = iCustomerServiceFront.createAccount(customer);
      return account;
  }
//  前台邮箱登录
  @RequestMapping("/emailLogin")
  @ResponseBody
  @Log(operationName = "用户邮箱登录",operationType = "用户")
  public Map<String, Object> loginAccount(Customer customer,HttpServletRequest request){
    Map<String, Object> account = iCustomerServiceFront.loginAccount(customer);
    Customer customer1 = iCustomerServiceFront.selectOneByEmail(customer.getEmail());
    request.getSession().setAttribute("customer",customer1);
    return account;
  }

//  激活账户
  @RequestMapping("/activation")
  @ResponseBody
  @Log(operationName = "激活账户",operationType = "系统")
  public Map<String, Object> activationAccount(String confirmCode){
    return iCustomerServiceFront.activationAccount(confirmCode);
  }


//前台登录
  @RequestMapping("/login")
  @ResponseBody
  @Log(operationName = "用户普通登录",operationType = "用户")
  public String login(Customer customer,HttpServletRequest request){
    String flag = "error";
    customer.setPassword(new MD5Code().getMD5ofStr("{[" + customer.getPassword() + "]}" + customer.getCuid()));
    Customer customer1 = iCustomerServiceFront.login(customer);
    if(customer1 != null){
      request.getSession().setAttribute("customer",customer1);
      flag = "success";
    }
    return flag;
}

  //  修改密码
  @RequestMapping("/pages/front/customer/updatePasswordPre")
  @Log(operationName = "用户修改密码页面",operationType = "用户")
  public String updatePasswordPre() {
    return "/pages/front/customer/updatePassword";
  }

  @RequestMapping("/pages/front/customer/updatePassword")
  @ResponseBody
  @Log(operationName = "用户修改密码",operationType = "系统")
  public String updatePassword(String password,HttpServletRequest request) {
    Customer customer = (Customer) request.getSession().getAttribute("customer");
    String cuid = customer.getCuid();
    String pass = new MD5Code().getMD5ofStr("{[" + password + "]}" + cuid); // 密码进行加密处理
    if (iCustomerServiceFront.updateByPassword(cuid, pass)) {
      return "success";
    }
    return "error";
  }



//  后台管理


  @RequestMapping("/pages/back/customer/listSplitPre")
  @Log(operationName = "查询所有用户前",operationType = "管理员")
  public String listSplitPre(){
    return "/pages/back/customer/list";
  }

  @RequestMapping("/pages/back/customer/listSplit")
  @ResponseBody
  @Log(operationName = "查询所有用户",operationType = "系统")
  public Map<String, Object> listSplit(Customer customer, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iCustomerServiceBack.listSplit(currentPage,lineSize);
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

//模糊查询
  @RequestMapping("/pages/back/Customer/findByName")
  @ResponseBody
  @Log(operationName = "模糊查询用户",operationType = "管理员")
  public Map<String, Object> findByName(String name, HttpServletRequest request){
//    String name1 = request.getParameter("name");
//    System.out.println(name);
    int currentPage = 1;
    int lineSize = 10;
//    System.out.println(name1+"controller");
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iCustomerServiceBack.findByName(currentPage,lineSize,"%" + name + "%");
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

  @RequestMapping("/pages/back/Customer/delete")
  @ResponseBody
  @Log(operationName = "删除用户",operationType = "管理员")
  public String delete(String cuid){
    String flag = "error";
    if(iCustomerServiceBack.delete(cuid)){
      return "success";
    }
    return flag;
  }

  @RequestMapping("/pages/back/Customer/delAll")
  @ResponseBody
  @Log(operationName = "批量删除用户",operationType = "管理员")
  public String delAll(String ids) {
    String flag = "success";
    List<String> cuid = new ArrayList<>();
    String[] strs = ids.split(",");
    for (String str : strs) {
      iCustomerServiceBack.delete(str);
    }
    return flag;
  }

  //  登录注销
  @RequestMapping("/pages/customerLogOut")
  @Log(operationName = "用户注销",operationType = "用户")
  public String logOut(HttpServletRequest request){
    request.getSession().invalidate();
    return "/pages/index";
  }
}
